#!/usr/bin/env python
# -*- coding: utf-8 -*-

# subflip - Prepare subtitle files for non-bidi-aware players.
# Copyright (C) 2006 Noam Raphael < spam.noam at gmail.com >
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import unittest

from bidi_algorithm import tag_text, OPEN_L, CLOSE_L, OPEN_R, CLOSE_R

const_conv = {OPEN_L: '<l>', CLOSE_L: '</l>', OPEN_R: '<r>', CLOSE_R: '</r>'}
def tag_string(s, is_ltr):
    return ''.join(const_conv.get(x, x) for x in tag_text(s, is_ltr))

class LTRTestCase(unittest.TestCase):

    def testSimple(self):
        self.assertEqual(
            tag_string(u'hello שלום hello', True),
            u'hello <r>שלום</r> hello')

    def testInverse(self):
        self.assertEqual(
            tag_string(u'שלום hello שלום', True),
            u'<r>שלום</r> hello <r>שלום</r>')

    def testEngComplex(self):
        self.assertEqual(
            tag_string(u'Lets combine numbers: 13, 02-12345, -8$. '
                       u'And now, מה הייתם אומרים על 19, is it nice?',
                       True),
            u'Lets combine numbers: 13, 02-12345, -8$. '
            u'And now, <r>מה הייתם אומרים על <l>19</l></r>, is it nice?')

    def testHebComplex(self):
        self.assertEqual(
            tag_string(u'הבה ונשלב מעט מספרים: 13, 02-12345. -8$. '
                       u'וכעת, What would you say about 19, האם זה מספר יפה?',
                       True),
            u'<r>הבה ונשלב מעט מספרים: <l>13</l>, <l>02-12345</l>. -<l>8$</l>. '
            u'וכעת</r>, What would you say about 19, <r>האם זה מספר יפה</r>?')

class RTLTestCase(unittest.TestCase):

    def testSimple(self):
        self.assertEqual(
            tag_string(u'שלום hello שלום', False),
            u'שלום <l>hello</l> שלום')

    def testInverse(self):
        self.assertEqual(
            tag_string(u'hello שלום hello', False),
            u'<l>hello</l> שלום <l>hello</l>')

    def testHebComplex(self):
        self.assertEqual(
            tag_string(u'הבה ונשלב מעט מספרים: 13, 02-12345. -8$. '
                       u'וכעת, What would you say about 19, האם זה מספר יפה?',
                       False),
            u'הבה ונשלב מעט מספרים: <l>13</l>, <l>02-12345</l>. -<l>8$</l>. '
            u'וכעת, <l>What would you say about 19</l>, האם זה מספר יפה?')

    def testEngComplex(self):
        self.assertEqual(
            tag_string(u'Lets combine numbers: 13, 02-12345, -8$. '
                       u'And now, מה הייתם אומרים על 19, is it nice?',
                       False),
            u'<l>Lets combine numbers: 13, 02-12345, -8$. '
            u'And now</l>, מה הייתם אומרים על <l>19</l>, <l>is it nice</l>?')

if __name__ == '__main__':
    unittest.main()
